package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import org.example.domain.dto.MoneyType;
import org.example.domain.dto.SerialFindCondition;
import org.example.domain.entity.Serial;
import org.example.domain.model.PageData;
import org.example.domain.model.SerialPage;
import org.example.mapper.SerialMapper;
import org.example.service.ISerialService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SerialServiceImpl implements ISerialService {

    private SerialMapper serialMapper;

    public SerialServiceImpl(SerialMapper serialMapper) {
        this.serialMapper = serialMapper;
    }


    @Override
    public SerialPage getPageSerialInfo(Integer page, Integer limit) {
        Page<Serial> serials = PageHelper.startPage(page, limit);
        QueryWrapper<Serial> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("gmt_payment");
        serialMapper.selectList(queryWrapper);
        SerialPage serialPage = new SerialPage();
        serialPage.setData(serials);
        serialPage.setCurrentPage(page);
        serialPage.setTotal(serials.getTotal());
        serialPage.setMoneyType(this.findMoneyTypeAll());
        return serialPage;
    }

    @Override
    public SerialPage finSerialInfo(SerialFindCondition serialFindCondition) {
        List<Serial> serials = serialMapper.findByGmtPaymentAndSerialId(serialFindCondition);
        SerialPage serialPage = new SerialPage();
        serialPage.setData(serials);
        serialPage.setMoneyType(this.findMoneyTypeByCondition(serialFindCondition));
        serialPage.setTotal((long) this.findMoneyTypeByCondition(serialFindCondition).getTotal());
        return serialPage;
    }

    public MoneyType findMoneyTypeAll(){
        MoneyType moneyType = new MoneyType();
        List<Serial> serials = serialMapper.selectList(null);
        serials.forEach(res -> {
            moneyType.setTotalMoney(moneyType.getTotalMoney() + Double.parseDouble(res.getReceiptAmount()));
            if (res.getPayType() == 0){
                moneyType.setZfbMoney(moneyType.getZfbMoney() + Double.parseDouble(res.getReceiptAmount()));
            }else{
                moneyType.setVxMoney(moneyType.getVxMoney() + Double.parseDouble(res.getReceiptAmount()));
            }
        });
        return moneyType;
    }

    public MoneyType findMoneyTypeByCondition(SerialFindCondition serialFindCondition){
        MoneyType moneyType = new MoneyType();
        List<Serial> serials = serialMapper.findMoneyTypeByCondition(serialFindCondition);
        serials.forEach(res -> {
            moneyType.setTotalMoney(moneyType.getTotalMoney() + Double.parseDouble(res.getReceiptAmount()));
            if (res.getPayType() == 0){
                moneyType.setZfbMoney(moneyType.getZfbMoney() + Double.parseDouble(res.getReceiptAmount()));
            }else{
                moneyType.setVxMoney(moneyType.getVxMoney() + Double.parseDouble(res.getReceiptAmount()));
            }
        });
        moneyType.setTotal(serials.size());
        return moneyType;
    }

}
